const { app, BrowserWindow, Tray,Menu,ipcMain } = require('electron')
const path = require('path')

let win
let tray
function createWindow () {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    resizable:false,
    frame: false,
    webPreferences: {
    //   preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      contextIsolation:false
    }
  })
  win.loadFile('index.html')
  ipcMain.on("hideWin",(event,data) => {
    win.hide();
  })
}

app.whenReady().then(() => {
  tray = new Tray (path.join(__dirname, 'm.png' )) // sets tray icon image
  const contextMenu = Menu.buildFromTemplate([
      {
          label: '退出',
          click: function () {
            app.quit();
            app.quit();//因为程序设定关闭为最小化，所以调用两次关闭，防止最大化时一次不能关闭的情况
          }
      }
  ])
  tray.setContextMenu(contextMenu)
  tray.on('click',function(){
      win.show();
  })
  createWindow()
  // app.on('activate', () => {
  //   if (BrowserWindow.getAllWindows().length === 0) {
  //     createWindow()
  //   }
  // })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

//electron-builder --win
//electron-builder --mac
